package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetmelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐管理接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmelService setmealService;




    /**
     * 套餐分页查询
     * @return
     */
    @ApiOperation("套餐分页查询接口")
    @GetMapping("/page")
    public Result listPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询");
        PageResult pageResult = setmealService.pageQuer(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐：{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有缓存
   public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("接收到的Status是{}，id是：{}",status,id);
        setmealService.startOrStop(status,id);
        return Result.success();
   }

   @ApiOperation("批量删除套餐")
    @DeleteMapping
   @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有缓存
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除：{}", ids);
        setmealService.delete(ids);
        return Result.success();
   }

   /**
    * 根据id查询套餐和套餐内菜品信息
    * @param id
    * @return
    */

   @ApiOperation("根据id查询套餐和套餐内菜品信息")
    @GetMapping("/{id}")
    public Result<SetmealDTO> getById(@PathVariable Long id){
       log.info("根据id查询套餐和套餐内菜品信息：{}", id);
       SetmealDTO setmealDTO = setmealService.getById(id);
       return Result.success(setmealDTO);
   }

   /**
    * 修改套餐数据
    * @param setmealDTO
    * @return
    */
   @ApiOperation("修改套餐数据")
    @PutMapping
   @CacheEvict(cacheNames = "setmeal",allEntries = true)//删除所有缓存
    public Result update(@RequestBody SetmealDTO setmealDTO){
       log.info("修改套餐数据：{}", setmealDTO);
       setmealService.update(setmealDTO);
       return Result.success();
   }
}
